/**
 * Filename: AlbumContentService.java
 *
 * @author Firefly
 * @contact postfirefly@163.com
 * @time 2016/3/8 12:52
 */
package com.github.postsanf.yinian.service;

import android.content.Intent;
import android.util.Log;

import com.github.postsanf.yinian.BaseIntentService;
import com.github.postsanf.yinian.R;
import com.github.postsanf.yinian.bean.YNPhoto;
import com.github.postsanf.yinian.bean.YNStatus;
import com.github.postsanf.yinian.constants.ACTIONs;
import com.github.postsanf.yinian.constants.CommonConstants;
import com.github.postsanf.yinian.constants.YNSingleton;
import com.github.postsanf.yinian.entity.YNAlbumMemberResponse;
import com.github.postsanf.yinian.entity.YNPhotoResponse;
import com.github.postsanf.yinian.entity.YNStatusResponse;
import com.github.postsanf.yinian.utils.ReqUtils;
import com.litesuits.http.exception.HttpException;
import com.litesuits.http.listener.HttpListener;
import com.litesuits.http.request.StringRequest;
import com.litesuits.http.request.param.HttpMethods;
import com.litesuits.http.response.Response;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

/**
 * 相册内容服务
 */
public class AlbumContentService extends BaseIntentService {

    public AlbumContentService() {
        super("AlbumContentService");
    }

    @Override
    public void onCreate() {
        super.onCreate();
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        if (intent != null) {
            final String action = intent.getAction();
            int groupid = intent.getExtras().getInt(ACTIONs.aGroupId);
            switch (action) {
                case ACTIONs.actionOfficialAlbumStatus:
                    int oid = intent.getExtras().getInt(ACTIONs.aLoadId);
                    String otype = intent.getExtras().getString(ACTIONs.aNeedType);
                    int isIn = intent.getExtras().getInt(ACTIONs.aIsInAlbum, 1);
                    boolean isTop = intent.getExtras().getBoolean("isTop");
                    getOfficialAlbumStatus(groupid, oid, otype, isIn, isTop);
                    break;
                // 获取动态
                case ACTIONs.actionAlbumStatus:
                    int sid = intent.getExtras().getInt(ACTIONs.aLoadId);
                    boolean update = intent.getExtras().getBoolean("update");
//                    getAlbumStatus(groupid, sid, update);
                    initTimelineStatus(groupid, sid, update);
                    break;
                // 刷新动态
                case ACTIONs.actionAlbumStatusRefresh:
                    int sid2 = intent.getExtras().getInt(ACTIONs.aLoadId);
//                    getAlbumStatusRefresh(groupid, reid);
                    initTimelineStatusRefresh(groupid, sid2);
                    break;
                // 获取照片墙
                case ACTIONs.actionAlbumWall:
                    String wtype = intent.getExtras().getString(ACTIONs.aNeedType);
                    int wid = intent.getExtras().getInt(ACTIONs.aLoadId);
                    getAlbumWall(groupid, wtype, wid);
                    break;
                // 获取成员
                case ACTIONs.actionAlbumMember:
                    getAlbumMember(groupid);
                    break;
            }
        }
    }

    // 刷新动态
    private void getAlbumStatusRefresh(int groupId, int loadId) {
        LinkedHashMap<String, String> mParam = new LinkedHashMap<>();
        mParam.put(CommonConstants.YNS_GROUP_ID, String.valueOf(groupId));
        mParam.put(CommonConstants.YNS_MAXEVENT_ID, String.valueOf(loadId));
        StringRequest req = new StringRequest(getString(R.string.BASE_URL) + getString(R.string.ynGroupInfoRefresh))
                .setMethod(HttpMethods.Post)
                .setParamMap(mParam)
                .setHttpListener(new HttpListener<String>(true, false, true) {
                    @Override
                    public void onSuccess(String s, Response<String> response) {
                        YNStatusResponse resAlbumStatusRe = gson.fromJson(s, YNStatusResponse.class);
                        if (resAlbumStatusRe != null && ReqUtils.isSuccess(resAlbumStatusRe.getCode())) {
                            List<YNStatus> statusLists = new ArrayList<>();
                            statusLists.addAll(resAlbumStatusRe.getData());
                            // 通知刷新更新
                            Intent upIntent = new Intent(ACTIONs.actionStatusUpdateRefresh);
                            upIntent.putExtra(ACTIONs.aAddData, (Serializable) statusLists);
                            sendBroadcast(upIntent);
                        }
                    }

                    @Override
                    public void onFailure(HttpException e, Response<String> response) {
                        showToast(CommonConstants.YN_NET_LINK);
                    }
                });
        liteHttp.executeAsync(req);
    }

    // 获取动态
    private void getAlbumStatus(int groupId, int loadId, final boolean update) {
        LinkedHashMap<String, String> mParam = new LinkedHashMap<>();
        mParam.put(CommonConstants.YNS_USER_ID, String.valueOf(application.getCurID()));
        mParam.put(CommonConstants.YNS_GROUP_ID, String.valueOf(groupId));
        mParam.put(CommonConstants.YNS_MIN_ID, String.valueOf(loadId));
        StringRequest req = new StringRequest(getString(R.string.BASE_URL) + getString(R.string.ynShowGroupInfo))
                .setMethod(HttpMethods.Post)
                .setParamMap(mParam)
                .setHttpListener(new HttpListener<String>(true, false, true) {
                    @Override
                    public void onSuccess(String s, Response<String> response) {
                        Log.i("yuhao", "timeline===status==>" + s);
                        YNStatusResponse resAlbumStatus = gson.fromJson(s, YNStatusResponse.class);
                        if (resAlbumStatus != null && ReqUtils.isSuccess(resAlbumStatus.getCode())) {
//                            List<YNStatus> statusLists = new ArrayList<>();
//                            statusLists.addAll(resAlbumStatus.getData());
                            YNSingleton.getInstance().clearStatusList();
                            YNSingleton.getInstance().setStatusList(resAlbumStatus.getData());
                            // 通知动态更新
//                            YNStatusList ynStatusList = new YNStatusList();
//                            ynStatusList.getYNStatuslist().addAll(statusLists);
//                            application.saveObject(ynStatusList, "YNStatusList");
                            Intent upIntent = new Intent(ACTIONs.actionStatusUpdate);
                            upIntent.putExtra("update", update);
                            sendBroadcast(upIntent);
                        }
                    }

                    @Override
                    public void onFailure(HttpException e, Response<String> response) {
//                        showToast(CommonConstants.YN_NET_LINK);
                        Log.i("xjs", "ffffff");
//                        YNStatusList ynStatusList = (YNStatusList) application.readObject("YNStatusList");
//                        YNSingleton.getInstance().clearStatusList();
//                        YNSingleton.getInstance().setStatusList(ynStatusList.getYNStatuslist());
//                        Intent upIntent = new Intent(ACTIONs.actionStatusUpdate);
//                        sendBroadcast(upIntent);
                    }
                });
        liteHttp.executeAsync(req);
    }

    private void initTimelineStatus(int groupId, int loadId, final boolean update) {
        LinkedHashMap<String, String> mParam = new LinkedHashMap<>();
        mParam.put(CommonConstants.YNS_USER_ID, String.valueOf(application.getCurID()));
        mParam.put(CommonConstants.YNS_GROUP_ID, String.valueOf(groupId));
        mParam.put(CommonConstants.YNS_EID, String.valueOf(loadId));
        if (loadId != 0) {
            mParam.put(CommonConstants.YNS_TYPE, "loading");
        } else {
            mParam.put(CommonConstants.YNS_TYPE, "initialize");
        }
        StringRequest req = new StringRequest("https://api.zhuiyinanian.com/YinianProject/event/ShowTimeAxis")
                .setMethod(HttpMethods.Post)
                .setParamMap(mParam)
                .setHttpListener(new HttpListener<String>(true, false, true) {
                    @Override
                    public void onSuccess(String s, Response<String> response) {
                        Log.i("xjs", "initTimelineStatus===>" + s);
                        YNStatusResponse resAlbumStatus = gson.fromJson(s, YNStatusResponse.class);
                        if (resAlbumStatus != null && ReqUtils.isSuccess(resAlbumStatus.getCode())) {
                            YNSingleton.getInstance().clearStatusList();
                            YNSingleton.getInstance().setStatusList(resAlbumStatus.getData());
                            // 通知动态更新
                            Intent upIntent = new Intent(ACTIONs.actionStatusUpdate);
                            upIntent.putExtra("update", update);
                            sendBroadcast(upIntent);
                        }
                    }

                    @Override
                    public void onFailure(HttpException e, Response<String> response) {
//                        showToast(CommonConstants.YN_NET_LINK);
                    }
                });
        liteHttp.executeAsync(req);
    }

    private void initTimelineStatusRefresh(int groupId, int loadId) {
        LinkedHashMap<String, String> mParam = new LinkedHashMap<>();
        mParam.put(CommonConstants.YNS_USER_ID, String.valueOf(application.getCurID()));
        mParam.put(CommonConstants.YNS_GROUP_ID, String.valueOf(groupId));
        mParam.put(CommonConstants.YNS_EID, String.valueOf(loadId));
        mParam.put(CommonConstants.YNS_TYPE, "refresh");
        StringRequest req = new StringRequest("https://api.zhuiyinanian.com/YinianProject/event/ShowTimeAxis")
                .setMethod(HttpMethods.Post)
                .setParamMap(mParam)
                .setHttpListener(new HttpListener<String>(true, false, true) {
                    @Override
                    public void onSuccess(String s, Response<String> response) {
                        YNStatusResponse resAlbumStatusRe = gson.fromJson(s, YNStatusResponse.class);
                        if (resAlbumStatusRe != null && ReqUtils.isSuccess(resAlbumStatusRe.getCode())) {
                            List<YNStatus> statusLists = new ArrayList<>();
                            statusLists.addAll(resAlbumStatusRe.getData());
                            // 通知刷新更新
                            Intent upIntent = new Intent(ACTIONs.actionStatusUpdateRefresh);
                            upIntent.putExtra(ACTIONs.aAddData, (Serializable) statusLists);
                            sendBroadcast(upIntent);
                        }
                    }

                    @Override
                    public void onFailure(HttpException e, Response<String> response) {
                        showToast(CommonConstants.YN_NET_LINK);
                    }
                });
        liteHttp.executeAsync(req);
    }

    // 获取官方相册动态
    private void getOfficialAlbumStatus(int groupId, int loadId, String type, int isIn, final boolean isTop) {
        String address = getString(R.string.ynShowOfficialAlbum);
        LinkedHashMap<String, String> mParam = new LinkedHashMap<>();
        if (isIn == 0) {
            address = getString(R.string.ynShowOfficialAlbumWhenNotIn);
        } else {
            mParam.put(CommonConstants.YNS_USER_ID, String.valueOf(application.getCurID()));
        }
        mParam.put(CommonConstants.YNS_GROUP_ID, String.valueOf(groupId));
        mParam.put(CommonConstants.YNS_EVENT_IDD, String.valueOf(loadId));
        mParam.put(CommonConstants.YNS_SIGN, type);
        StringRequest req = new StringRequest(getString(R.string.BASE_URL) + address)
                .setMethod(HttpMethods.Post)
                .setParamMap(mParam)
                .setHttpListener(new HttpListener<String>(true, false, true) {
                    @Override
                    public void onSuccess(String s, Response<String> response) {
                        Log.i("xjs", "offficessssss====>" + s);
                        YNStatusResponse resOfficialStatus = gson.fromJson(s, YNStatusResponse.class);
                        if (resOfficialStatus != null && ReqUtils.isSuccess(resOfficialStatus.getCode())) {
                            List<YNStatus> statusLists = new ArrayList<>();
                            statusLists.addAll(resOfficialStatus.getData());
                            YNSingleton.getInstance().setStatusList(statusLists);

                            // 通知动态更新
                            Intent upIntent = new Intent(ACTIONs.actionOfficialStatusUpdate);
                            upIntent.putExtra("isTop", isTop);
                            sendBroadcast(upIntent);
                        }
                    }

                    @Override
                    public void onFailure(HttpException e, Response<String> response) {
                        showToast(CommonConstants.YN_NET_LINK);
                    }
                });
        liteHttp.executeAsync(req);
    }

    // 获取照片墙
    private void getAlbumWall(int groupId, final String type, int loadId) {
        LinkedHashMap<String, String> mParam = new LinkedHashMap<>();
        mParam.put(CommonConstants.YNS_GROUP_ID, String.valueOf(groupId));
        mParam.put(CommonConstants.YNS_INFO_TYPE, type);
        mParam.put(CommonConstants.YNS_ID, String.valueOf(loadId));
        StringRequest req = new StringRequest(getString(R.string.BASE_URL) + getString(R.string.ynGroupAlbum))
                .setMethod(HttpMethods.Post)
                .setParamMap(mParam)
                .setHttpListener(new HttpListener<String>(true, false, true) {
                    @Override
                    public void onSuccess(String s, Response<String> response) {
                        YNPhotoResponse resAlbumWall = gson.fromJson(s, YNPhotoResponse.class);
                        if (resAlbumWall != null && ReqUtils.isSuccess(resAlbumWall.getCode())) {
                            List<YNPhoto> photoLists = new ArrayList<>();
                            photoLists.addAll(resAlbumWall.getData());
                            String direction = ACTIONs.aAddBottom;
                            switch (type) {
                                // 初始化
                                case CommonConstants.YNS_SIGN_INITS:
                                    direction = ACTIONs.aAddBottom;
                                    break;
                                // 加载
                                case CommonConstants.YNS_SIGN_LOAD:
                                    direction = ACTIONs.aAddBottom;
                                    break;
                                // 刷新
                                case CommonConstants.YNS_SIGN_REFRESH:
                                    direction = ACTIONs.aAddTop;
                                    break;
                            }
                            // 通知照片墙更新
                            Intent upIntent = new Intent(ACTIONs.actionWallUpdate);
                            upIntent.putExtra(ACTIONs.aAddData, (Serializable) photoLists);
                            upIntent.putExtra(ACTIONs.aAddDirection, direction);
                            sendBroadcast(upIntent);

                            Intent intent = new Intent(ACTIONs.actionTransfer);
                            intent.putExtra(ACTIONs.aAddData, (Serializable) photoLists);
                            intent.putExtra(ACTIONs.aAddDirection, direction);
                            sendBroadcast(intent);
                        }
                    }

                    @Override
                    public void onFailure(HttpException e, Response<String> response) {
//                        showToast(CommonConstants.YN_NET_LINK);
                    }
                });
        liteHttp.executeAsync(req);
    }

    // 获取成员
    private void getAlbumMember(int groupid) {
        LinkedHashMap<String, String> mParam = new LinkedHashMap<>();
        mParam.put(CommonConstants.YNS_GROUP_ID, String.valueOf(groupid));
        mParam.put(CommonConstants.YNS_USER_ID, String.valueOf(application.getCurID()));
        StringRequest req = new StringRequest(getString(R.string.BASE_URL) + getString(R.string.ynShowMember))
                .setMethod(HttpMethods.Post)
                .setParamMap(mParam)
                .setHttpListener(new HttpListener<String>(true, false, true) {
                    @Override
                    public void onSuccess(String s, Response<String> response) {
                        YNAlbumMemberResponse resAlbumMember = gson.fromJson(s, YNAlbumMemberResponse.class);
                        if (resAlbumMember != null && ReqUtils.isSuccess(resAlbumMember.getCode())) {
//                            List<YNMember> memberLists = new ArrayList<>();
//                            memberLists.clear();
//                            memberLists.addAll(resAlbumMember.getData());
//                            Log.i("xjs","member+s===>" + s);
                            // 通知成员更新
                            Intent upIntent = new Intent(ACTIONs.actionMemberUpdate);
                            upIntent.putExtra(ACTIONs.aAddData, (Serializable) resAlbumMember.getData());
                            sendBroadcast(upIntent);
                            application.setMemberList(resAlbumMember.getData());
                        }
                    }

                    @Override
                    public void onFailure(HttpException e, Response<String> response) {
//                        showToast(CommonConstants.YN_NET_LINK);
                    }
                });
        liteHttp.executeAsync(req);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }
}
